home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000054.txt < prev    next >
Text File  |  2013-04-03  |  4KB  |  146 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Shim that simulates a <webview> tag via Mutation Observers.
  6. //
  7. // The actual tag is implemented via the browser plugin. The internals of this
  8. // are hidden via Shadow DOM.
  9.  
  10. var watchForTag = require("tagWatcher").watchForTag;
  11.  
  12. var WEB_VIEW_ATTRIBUTES = ['src', 'partition'];
  13.  
  14. // All exposed api methods for <webview>, these are forwarded to the browser
  15. // plugin.
  16. var WEB_VIEW_API_METHODS = [
  17.   'back',
  18.   'canGoBack',
  19.   'canGoForward',
  20.   'forward',
  21.   'getProcessId',
  22.   'go',
  23.   'reload',
  24.   'stop',
  25.   'terminate'
  26. ];
  27.  
  28. var WEB_VIEW_EVENTS = {
  29.   'exit' : ['processId', 'reason'],
  30.   'loadabort' : ['url', 'isTopLevel', 'reason'],
  31.   'loadcommit' : ['url', 'isTopLevel'],
  32.   'loadredirect' : ['oldUrl', 'newUrl', 'isTopLevel'],
  33.   'loadstart' : ['url', 'isTopLevel'],
  34.   'loadstop' : [],
  35.   'sizechanged': ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'],
  36. };
  37.  
  38. window.addEventListener('DOMContentLoaded', function() {
  39.   watchForTag('WEBVIEW', function(addedNode) { new WebView(addedNode); });
  40. });
  41.  
  42. /**
  43.  * @constructor
  44.  */
  45. function WebView(node) {
  46.   this.node_ = node;
  47.   var shadowRoot = node.webkitCreateShadowRoot();
  48.  
  49.   this.objectNode_ = document.createElement('object');
  50.   this.objectNode_.type = 'application/browser-plugin';
  51.   // The <object> node fills in the <browser> container.
  52.   this.objectNode_.style.width = '100%';
  53.   this.objectNode_.style.height = '100%';
  54.   WEB_VIEW_ATTRIBUTES.forEach(function(attributeName) {
  55.     this.objectNode_.setAttribute(
  56.         attributeName, this.node_.getAttribute(attributeName));
  57.   }, this);
  58.  
  59.   shadowRoot.appendChild(this.objectNode_);
  60.  
  61.   // this.objectNode_[apiMethod] are defined after the shadow object is appended
  62.   // to the shadow root.
  63.   WEB_VIEW_API_METHODS.forEach(function(apiMethod) {
  64.     node[apiMethod] = this.objectNode_[apiMethod].bind(this.objectNode_);
  65.   }, this);
  66.  
  67.   // Map attribute modifications on the <webview> tag to property changes in
  68.   // the underlying <object> node.
  69.   var handleMutation = this.handleMutation_.bind(this);
  70.   var observer = new WebKitMutationObserver(function(mutations) {
  71.     mutations.forEach(handleMutation);
  72.   });
  73.   observer.observe(
  74.       this.node_,
  75.       {attributes: true, attributeFilter: WEB_VIEW_ATTRIBUTES});
  76.  
  77.   var handleObjectMutation = this.handleObjectMutation_.bind(this);
  78.   var objectObserver = new WebKitMutationObserver(function(mutations) {
  79.     mutations.forEach(handleObjectMutation);
  80.   });
  81.   objectObserver.observe(
  82.       this.objectNode_,
  83.       {attributes: true, attributeFilter: WEB_VIEW_ATTRIBUTES});
  84.  
  85.   var objectNode = this.objectNode_;
  86.   // Expose getters and setters for the attributes.
  87.   WEB_VIEW_ATTRIBUTES.forEach(function(attributeName) {
  88.     Object.defineProperty(this.node_, attributeName, {
  89.       get: function() {
  90.         return objectNode[attributeName];
  91.       },
  92.       set: function(value) {
  93.         objectNode[attributeName] = value;
  94.       },
  95.       enumerable: true
  96.     });
  97.   }, this);
  98.  
  99.   // We cannot use {writable: true} property descriptor because we want dynamic
  100.   // getter value.
  101.   Object.defineProperty(this.node_, 'contentWindow', {
  102.     get: function() {
  103.       // TODO(fsamuel): This is a workaround to enable
  104.       // contentWindow.postMessage until http://crbug.com/152006 is fixed.
  105.       return objectNode.contentWindow.self;
  106.     },
  107.     // No setter.
  108.     enumerable: true
  109.   });
  110.  
  111.   for (var eventName in WEB_VIEW_EVENTS) {
  112.     this.setupEvent_(eventName, WEB_VIEW_EVENTS[eventName]);
  113.   }
  114. }
  115.  
  116. /**
  117.  * @private
  118.  */
  119. WebView.prototype.handleMutation_ = function(mutation) {
  120.   this.objectNode_[mutation.attributeName] =
  121.       this.node_.getAttribute(mutation.attributeName);
  122. };
  123.  
  124. /**
  125.  * @private
  126.  */
  127. WebView.prototype.handleObjectMutation_ = function(mutation) {
  128.   this.node_.setAttribute(mutation.attributeName,
  129.       this.objectNode_.getAttribute(mutation.attributeName));
  130. };
  131.  
  132. /**
  133.  * @private
  134.  */
  135. WebView.prototype.setupEvent_ = function(eventname, attribs) {
  136.   var node = this.node_;
  137.   this.objectNode_.addEventListener('-internal-' + eventname, function(e) {
  138.     var evt = new Event(eventname);
  139.     var detail = e.detail ? JSON.parse(e.detail) : {};
  140.     attribs.forEach(function(attribName) {
  141.       evt[attribName] = detail[attribName];
  142.     });
  143.     node.dispatchEvent(evt);
  144.   });
  145. }
  146.